package server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import plug.result.ReqPage;
import plug.result.RetResponse;
import plug.result.RetResult;
import server.dao.DyLogDao;
import server.dao.MoneyRecordDao;
import server.dao.TenantDao;
import server.model.entity.DyLog;
import server.model.entity.MoneyRecord;
import server.model.entity.Tenant;
import server.model.entity.Users;

import java.util.Optional;

/**
 * @description:
 * @author: 许哲
 * @createTime: 2021/11/22 21:13
 * @version: 1.0
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("v1/api/record")
public class RecordController {
    @Autowired
    private MoneyRecordDao moneyRecordDao;
    @Autowired
    private DyLogDao dyLogDao;
    @Autowired
    private TenantDao tenantDao;

    @PostMapping("/all")
    public RetResult all(@RequestBody ReqPage reqPage) {
        Users users = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        MoneyRecord entity = JSON.parseObject(req.toJSONString(), MoneyRecord.class);
        if (users.getType().equalsIgnoreCase("USER")) {
            Tenant tenant = tenantDao.findByUserId(users.getId());
            entity.setSystemCode(tenant.getCode());
        }
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("systemCode", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<MoneyRecord> example = Example.of(entity, matcher);
        Page<MoneyRecord> pages = moneyRecordDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }

    @PostMapping("/log/all")
    public RetResult logAll(@RequestBody ReqPage reqPage) {
        Users users = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        DyLog entity = JSON.parseObject(req.toJSONString(), DyLog.class);
        if (users.getType().equalsIgnoreCase("USER")) {
            Tenant tenant = tenantDao.findByUserId(users.getId());
            entity.setSystemCode(tenant.getCode());
        }
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("uuid", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<DyLog> example = Example.of(entity, matcher);
        Page<DyLog> pages = dyLogDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }
}
